//////////////////////////////////////////////////////////////////////
// DeadTouch - third line of the legendary OpenTibia Server
//////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//////////////////////////////////////////////////////////////////////

#include "position.h"
#include "configmanager.h"
#include <vector>
#include "marks.h"
#include "tools.h"

#include <libxml/xmlmemory.h>
#include <libxml/parser.h>

extern ConfigManager g_config;

bool Marks::loadFromXml( std::string filename )
{
     xmlDocPtr doc = xmlParseFile( (g_config.getString( ConfigManager::DATA_DIRECTORY ) + "XML/" + filename).c_str() );
     
     if(!doc)
        return false;
        
     xmlNodePtr root, p;
     
     root = xmlDocGetRootElement(doc);
     
     if( xmlStrcmp( root->name, (const xmlChar*)"marks" ) != 0 )
     {
         xmlFreeDoc( doc );
         return false;
     }
     
     p = root->children;
     
     while( p )
     {
            int x = -1;
            int y = -1;
            int z = -1;
            std::string name = "";
            
            if( xmlStrcmp( p->name, (const xmlChar*)"mark" ) == 0 )
            {
                if( readXMLInteger( p, "x", x ) && readXMLInteger( p, "y", y ) && readXMLInteger( p, "z", z ) && readXMLString( p, "name", name ) )
                    addMark( name, Position(x,y,z) );
            }
            
            p = p->next;
     }
     
     xmlFreeDoc( doc );
     
     return true;
}
